home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Pascal / Snippets / OpenSelection / OpenSelection.p next >
Encoding:
Text File  |  1994-05-03  |  6.3 KB  |  182 lines  |  [TEXT/R*ch]

  1. { OpenSelection.p ported by Matthew Xavier Mora} 
  2. { ported from C.K. Han's openselection.c code  }
  3. { 09-11-92                                     }
  4.  
  5. unit openSelection; 
  6. interface
  7.   uses
  8.     AppleTalk, PPCToolBox, Processes, EPPC, Notification, AppleEvents,
  9.     {, Errors, Events, Memory, Resources, SegLoad, }
  10.  
  11.     (* Group 3 *)
  12.     Aliases {, thermometer};
  13.  
  14.   function OpenSelection (var theFileToOpen: FSSpec): OSErr;
  15.  
  16. implementation
  17.   const
  18.     kFinderType = 'FNDR';
  19.     kSysCreator = 'MACS';
  20.     kAEOpenSelection = 'sope';
  21.     aeSelectionKeyword = 'fsel';
  22.  
  23. { This runs through the process list looking for the indicated application
  24. }
  25.   function FindAProcess (typeToFind, creatorToFind: Ostype;
  26.                          var processSN:ProcessSerialNumber;
  27.                          var infoRecToFill: ProcessInfoRec): OSErr;
  28.     var
  29.       tempPSN: ProcessSerialNumber;
  30.       myErr: OSErr;
  31.  
  32.   begin
  33.     myErr := noErr;
  34.     tempPSN.lowLongOfPSN := kNoProcess;
  35.     processSN.lowLongOfPSN := kNoProcess;
  36.     processSN.highLongOfPSN := kNoProcess;
  37.     repeat
  38.       begin
  39.         myErr := GetNextProcess(processSN);
  40.         if (myErr = noErr) then
  41.           myErr := GetProcessInformation(processSN, infoRecToFill);
  42.       end;
  43.     until ((infoRecToFill.processSignature = creatorToFind) or
  44.    (infoRecToFill.processType = Longint(typeToFind)) or (myErr <> noErr));
  45.  
  46.     FindAProcess := myErr;
  47.   end;
  48.  
  49.   function OpenSelection (var theFileToOpen: FSSpec): OSErr;
  50.     var
  51.       aeEvent, aeReply: AppleEvent;
  52.       aeDirDesc, listElem: AEDesc;
  53.       interactErr: OSErr;
  54.       dirSpec, procSpec: FSSpec;
  55.       fileList: AEDesc;
  56.       myReply: StandardFileReply;
  57.       myErr: OSErr;
  58.       process: ProcessSerialNumber;
  59.       DirAlias, FileAlias: AliasHandle;
  60.       infoRec: ProcessInfoRec;
  61.       processName: Str31;
  62.       fullPath, appName: Str255;
  63.       myAddressDesc: AEDesc;
  64.   begin
  65.     interactErr := noErr;
  66.  
  67.     if (true) then
  68.       begin
  69.         infoRec.processInfoLength := sizeof(ProcessInfoRec);
  70.         infoRec.processName := @processName;
  71.         infoRec.processAppSpec := @procSpec;
  72.  
  73.         myErr := FindAProcess(kFinderType, kSysCreator, process, infoRec);
  74.         if (myErr = noErr) then
  75.           myErr := AECreateDesc(typeProcessSerialNumber, @process,
  76.                                 sizeof(process), myAddressDesc);
  77.  
  78.  
  79.         if (myErr = noErr) then
  80.           begin
  81.  
  82.             { Create the FinderEvent }
  83.             myErr := AECreateAppleEvent(kFinderType, kAEOpenSelection,
  84.                                         myAddressDesc,
  85. kAutoGenerateReturnID,
  86.                                         kAnyTransactionID, aeEvent);
  87.             { If you want to keep talking to this machine, you can keep
  88. this  }
  89.             { address desc around }
  90.             myErr := AEDisposeDesc(myAddressDesc);
  91.  
  92.             if (myErr = noErr) then
  93.               begin
  94.                 { Now we build all the bits of an OpenSelection event. }
  95.                 { Basically, we need to create an alias for the item to
  96. open,}
  97.                 { and an alias to the parent }
  98.                 { folder (directory) of that item. }
  99.                 { We can also pass a list of files if we want.  }
  100.                 { You'll notice that for opening a finder window, the file
  101. and}
  102.                 { directory alias both point at the }
  103.                 { folder itself }
  104.                 { make a spec for the parent folder }
  105.  
  106.  
  107.                 myErr := FSMakeFSSpec(theFileToOpen.vRefNum,
  108.                                       theFileToOpen.parID, '', dirSpec);
  109.                 myErr := NewAlias(nil, dirSpec, DirAlias);
  110.  
  111.                 { Create alias for file }
  112.                 { if you are opening a window, then you make the file alias
  113. the}
  114.                 { same as the dir alias }
  115.                 myErr := NewAlias(nil, theFileToOpen, FileAlias);
  116.                 interactErr := AEInteractWithUser(kAEDefaultTimeout, nil,
  117. nil);
  118.                 if (interactErr = noErr) then
  119.                   myErr := SetFrontProcess(process);
  120.  
  121.                 { Create the file  list }
  122.                 if (myErr = noErr) then
  123.                   begin
  124.                     myErr := AECreateList(nil, 0, false, fileList);
  125.  
  126.                     {  create the folder  descriptor }
  127.                     HLock(Handle(DirAlias));
  128.                     myErr := AECreateDesc(typeAlias, Ptr(DirAlias^),
  129.                                           GetHandleSize(Handle(DirAlias)),
  130.                                           aeDirDesc);
  131.                     HUnlock(Handle(DirAlias));
  132.                     myErr := AEPutParamDesc(aeEvent, keyDirectObject,
  133.                                             aeDirDesc);
  134.                     if (myErr = noErr) then
  135.                       begin
  136.                         { done with the desc, kill it }
  137.                         myErr := AEDisposeDesc(aeDirDesc);
  138.                         {  create the file descriptor and add to aliasList
  139. }
  140.                         HLock(Handle(FileAlias));
  141.                         myErr := AECreateDesc(typeAlias, Ptr(FileAlias^),
  142.                                              
  143. GetHandleSize(Handle(FileAlias)),
  144.                                               listElem);
  145.                         HLock(Handle(FileAlias));
  146.                         myErr := AEPutDesc(fileList, 0, listElem);
  147.                       end;
  148.                     if (myErr = noErr) then
  149.                       begin
  150.  
  151.                         myErr := AEDisposeDesc(listElem);
  152.  
  153.                         { Add the file alias list to the event }
  154.                         myErr := AEPutParamDesc(aeEvent,
  155. aeSelectionKeyword,
  156.                                                 fileList);
  157.                         myErr := AEDisposeDesc(fileList);
  158.  
  159.                         if (myErr = noErr) then
  160.                           myErr := AESend(aeEvent, aeReply,
  161.                                           kAENoReply + kAEAlwaysInteract + 
  162.                                           kAECanSwitchLayer,
  163.                                           kAENormalPriority,
  164. kAEDefaultTimeout,
  165.                                           nil, nil);
  166.                       end;
  167.                   end;
  168.                 myErr := AEDisposeDesc(aeEvent);
  169.               end;
  170.           end;
  171.       end;
  172.     if (DirAlias <> nil) then
  173.       DisposHandle(Handle(DirAlias));
  174.     if (FileAlias <> nil) then
  175.       DisposHandle(Handle(FileAlias));
  176.     OpenSelection := myErr;
  177.   end;
  178.  
  179. end.
  180.  
  181.  
  182.